#include "chmbookdatabuilder.h"
#include "qtreemodelhandler.h"

extern void log(QString string);

CHMBookDataBuilder::CHMBookDataBuilder(const QString &fileName):BookDataBuilder(fileName)
{
}
void log(QString string){
    QFile file("log.txt");
    file.open(QFile::Append);
    file.write(string.toLocal8Bit().data(),string.toLocal8Bit().length());
    file.write("\n\r",2);
    file.close();
}
void CHMBookDataBuilder::parse()
{
    if(_bookData!=0)delete this->_bookData;
    this->_bookData = new BookData;
    QChm chm(_fileName);
    if(chm.open()){
        QString exePath = QCoreApplication::applicationDirPath ();
        QFileInfo fileInfo(_fileName);
        QDir qdir;

        qdir.setCurrent(exePath+"\\cache");
        QString newDirName123 = fileInfo.fileName().left(fileInfo.fileName().length()-4);
        if(!qdir.exists(newDirName123)){
            qdir.mkdir(newDirName123);
        }

        exePath.append("/cache/");
        exePath.append(newDirName123);
        //QMessageBox::about(0,"",exePath);
        chm.unzip(exePath);
    }
    chm.close();
}
void CHMBookDataBuilder::parse(QHHCHandler& handler){
    if(_bookData!=0)delete this->_bookData;
    this->_bookData = new BookData;
    QChm chm(_fileName);
    if(chm.open()){
        QString exePath = QCoreApplication::applicationDirPath ();
        QFileInfo fileInfo(_fileName);
        QDir qdir;

        qdir.setCurrent(exePath+"\\cache");
        QString newDirName123 = fileInfo.fileName().left(fileInfo.fileName().length()-4);
        if(!qdir.exists(newDirName123)){
            qdir.mkdir(newDirName123);
        }

        exePath.append("/cache/");
        exePath.append(newDirName123);

        log(QString("start unzip chm file.%1\n").arg(QDateTime::currentDateTime().toTime_t()));
        chm.unzip(exePath);
        log(QString("end unzip chm file.%1\n").arg(QDateTime::currentDateTime().toTime_t()));
        chm.getTopicsByHandler(handler);
        log(QString("end get topic chm file.%1\n").arg(QDateTime::currentDateTime().toTime_t()));
    }
    chm.close();
}
